<?php

    require_once("SmartyReady.php");
    require_once("usuario.php");
    require_once("mp3.php");
    // preparo l'smarty
    $sm = new SmartyReady();
    $sm->muntarTemplates();
    // preparo la sessio
    session_start();
    if (!empty($_SESSION['username'])) {
        $sm->assign('banner','bannerUser.tpl');
        $sm->assign('usuari',$_SESSION['username']);
        $sm->assign('pathAvatar',$_SESSION['avatar']);
        $sm->assign('cuerpo','cuerpoperfil.tpl');
        // Busquem totes les cançons de l'usuari, les parsejem amb el link i src corresponents
        $user = Usuario::findByUsername($_SESSION['username']);
        $playlist = $user->songs();
        $playlist_raw = Array();
        $playlist_tpl = Array();

        $index = 0;
        if (count($playlist) > 0) {
            foreach($playlist as $song) {
                $playlist_raw[]['title'] = $song->get_titol();
                $playlist_raw[$index]['author'] = $song->get_artista();
                $playlist_raw[$index]['file'] = $song->get_file();
                $playlist_tpl[] = '<a href="#" data-src="' . substr($playlist_raw[$index]['file'],11) . '">' .
                                                             $playlist_raw[$index]['title'] . '</a>';

                $index++;
            }
            $sm->assign('playlist',$playlist_tpl);
       }
       else $sm->assign('playlist','');
    }
    else {
        // T'has colat a la pagina del perfil sense estar logejat
        $sm->assign("banner","login.tpl");
        $sm->assign('resultatLogin','');
        $sm->assign('cuerpo','buit.tpl');
        ?><meta http-equiv="Refresh" content="0;url=./index.php"><?php     
    }
    $sm->display("E:/wamp/www/JediMusic/Smarty/templates/base.tpl");
?>
